//*****************************************************************************
// JFab (http://code.google.com/p/jfab)
// Copyright (c) 2011-2012 JFab.org
// Admin jfab@jeffreythompson.net
//
// See the file "LICENSE.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//*****************************************************************************

package org.jfab.core.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.Validate;
import org.jfab.core.model.CGType;
import org.jfab.core.model.CGTypeUtilities;

/**
 * Provides a map between a generics type parameter name and a code generator
 * type.
 */
public class TypeParameterMap
{
    /** Map. */
    private final Map<String, CGType> _map = new HashMap<String, CGType>();

    /** Code generator type utilities. */
    private final CGTypeUtilities _cgTypeUtils;

    /**
     * Construct this object with the given parameter.
     * 
     * @param cgTypeUtils Code generator type utilities. (required)
     */
    public TypeParameterMap(final CGTypeUtilities cgTypeUtils)
    {
        Validate.notNull(cgTypeUtils, "cgTypeUtils is null");

        _cgTypeUtils = cgTypeUtils;
    }

    /**
     * @param cgType Code generator type.
     * 
     * @return the code generator type mapped to the given parameter.
     */
    public CGType mapType(final CGType cgType)
    {
        CGType answer = null;

        if (cgType != null)
        {
            answer = get(_cgTypeUtils.toSimpleComponentNameString(cgType));

            if (answer == null)
            {
                answer = cgType;
            }
        }

        return answer;
    }

    /**
     * @param typeParameter Type parameter.
     * @param cgType Code generator type.
     */
    public void put(final String typeParameter, final CGType cgType)
    {
        _map.put(typeParameter, cgType);
    }

    /**
     * @param typeParameter Type parameter.
     * 
     * @return the code generator type mapped to the given parameter.
     */
    private CGType get(final String typeParameter)
    {
        return _map.get(typeParameter);
    }
}
